package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Contract;
import com.dragon.repository.entity.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 客户表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-16
 */
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {

    @Select("<script>"+
            "SELECT M.* from " +
            "(SELECT a.* , b.username as applicantName " +
            "FROM dragon_customer a  " +
            "LEFT JOIN dragon_admin b ON a.admin_id = b.id " +
            "LEFT JOIN dragon_admin_department c on c.admin_id =  b.id )M " +
            "${ew.customSqlSegment} " +
            "</script>" )
    @DataScope(deptAlias = "M",deptField = "department_id",adminAlias = "M",adminField = "admin_id", authCodeString = "customer")
    Page<Customer> selectCustomerList(Page<Customer> customerPage, @Param(Constants.WRAPPER) QueryWrapper<Customer> wrapper);

    /**
     * 假删客户信息
     * @param id
     */
    @Update("<script>" +
            "UPDATE dragon_customer c SET c.deleted = CASE c.deleted WHEN 0 THEN 1 END WHERE " +
            "<if test='id != null and id != 0'> "+
            "c.id = #{id} "+
            "</if>" +
            "</script> " )
    void fakeDelete(Integer id);

    /**
     * 根据客户id 查看合同详细数据
     * @param contractPage
     * @param customerId
     * @return
     */
    @Select("SELECT a.* FROM dragon_contract a WHERE a.customer_id = #{customerId}")
    Page<Contract> list(Page<Contract> contractPage, Integer customerId);

    @Select("<script>" +
            "select M.* from (SELECT DISTINCT a.* FROM dragon_customer a " +
            "LEFT JOIN dragon_contract b ON a.id = b.customer_id ) M where 1=1 " +
            "<if test='time != null and time != \"\"'>" +
            " and DATE_FORMAT(M.create_time,'%Y-%c') = #{time} or DATE_FORMAT(M.create_time,'%Y-%m') = #{time} " +
            "</if>" +
            "</script> ")
    @DataScope(deptAlias = "M",deptField = "department_id",adminAlias = "M",adminField = "admin_id", authCodeString = "customer")
    List<Customer> selectListByTime(String time);


    /**
     * 客户 数量
     * @param wrapper 条件
     * @return
     */
    @Select("SELECT count(*)  FROM dragon_customer ${ew.customSqlSegment} " )
    @DataScope(deptField = "department_id",adminField = "admin_id", authCodeString = "customer")
    Long selectCustomerCount(@Param(Constants.WRAPPER) QueryWrapper wrapper);


    @Select("select a.*, da.username as applicantName from dragon_customer a " +
            "left join dragon_admin da on a.admin_id = da.id where a.id = #{customerId} ")
    Customer selectDetailById(Integer customerId);


    @Select("SELECT b.id,b.customer_name,count(*) numbers FROM " +
            "(SELECT customer_id,order_time,status,deleted FROM dragon_contract) a " +
            "LEFT JOIN dragon_customer b on a.customer_id = b.id ${ew.customSqlSegment} group by b.id")
    Page<Customer> selectCountContractBy(Page<Customer> page,@Param(Constants.WRAPPER) QueryWrapper<Customer> wrapper);
}
